import { container } from "tsyringe";
import { Task } from "../../autox/task";
import { $, updateFloat } from "../../utils";
import { IScheduleTask } from "../node";
import { Thread } from "threads";
import { BackAction, ClickAction, ClickAndBackAction, GetMineMoneyAction, ToShouYeAction } from "./action";
import { Config } from "../../autox/config";

export class KwaiYuEScheduleTask implements IScheduleTask {
    time: number = 1000 * 60 * 1;
    title: string = `【查快手极速余额】`;
    isRuning: boolean = false;
    task: Task;
    packageName: string = $.PACKAGES["com.smile.gifmaker"];
    constructor() {
        this.task = container.resolve(Task)
    }
    check(): boolean {
        const config = container.resolve(Config)
        const storage: any = config.get(`MINE_TASK`) || {}
        if (storage && storage.kwaiCurrentMoney) {
            return false;
        }
        return true;
    }
    thread: Thread;
    start(): void {
        if (this.isRuning) {
            updateFloat({ toast: `${this.title}已经执行`, tip: '' })
            return;
        }
        this.isRuning = true;
        updateFloat({ toast: `正在执行${this.title}任务`, currentTask: this.title, tip: `正在打开应用` })
        if ($.getCurrentPackage() !== this.packageName) {
            $.launchPackage(this.packageName)
            // 等待10s 防止网络加载速度慢
            sleep(10 * 1000)
        }
        if (this.thread && this.thread.isAlive()) {
            this.thread.interrupt()
        }
        /**
         * 跳过障碍
         */
        this.thread = this.task.start(() => {
            while (true) {
                let nodes = selector().textMatches(/.{1,20}/).visibleToUser(true).find()
                nodes.map(node => {
                    const text = node.text()
                    if (text === '跳过' || text === '我知道了') {
                        new ClickAction(node).run()
                    }
                    if (text === '朋友推荐') {
                        new BackAction().run()
                    }
                    if (text === '点击立得奖励') {
                        new ClickAction(node).run()
                    }
                    if (text === '绑定手机号') {
                        new BackAction().run()
                    }
                    if (text.includes('邀新朋友')) {
                        new BackAction().run()
                    }
                    if (text === '立即领取') {
                        new ClickAndBackAction(node).run()
                    }
                    if (text === '去看看') {
                        new ClickAndBackAction(node).run()
                    }
                })
                sleep(1000)
            }
        });
        new GetMineMoneyAction(this.packageName).run()
        sleep(2000)
    }
    stop(): void {
        this.isRuning = false;
        if (this.thread && this.thread.isAlive()) {
            this.thread.interrupt()
        }
        updateFloat({ toast: `${this.title}任务执行结束`, currentTask: this.title, tip: '' })
        new ToShouYeAction(this.packageName).run()
    }
}

